package utils

import (
	"bytes"
	"errors"

	"github.com/goccy/go-json"
)

// JSONMarshal marshals v to JSON without escaping HTML
func JSONMarshal(v interface{}) ([]byte, error) {
	if v == nil {
		return nil, errors.New("v is nil")
	}
	var buf bytes.Buffer
	encoder := json.NewEncoder(&buf)
	encoder.SetEscapeHTML(false)
	err := encoder.Encode(v)
	if err != nil {
		return nil, err
	}
	// Remove the trailing newline added by encoder.Encode
	return bytes.TrimSpace(buf.Bytes()), nil
}

func ParseMap(data []byte) (map[string]interface{}, error) {
	var result map[string]interface{}
	err := json.Unmarshal(data, &result)
	if err != nil {
		return nil, err
	}
	return result, nil
}